import React, {useState} from "react";
import {defaultPagination, paginationTool} from "@/utils/util";
import {useMount, useRequest} from "ahooks";
import {
  adminConsignerPayment,
  cancelConsignerOrder,
  findByPageConsignerOrderList, shipmentsConsigner
} from "@/services/ant-design-pro/order";
import ProTable from "@ant-design/pro-table";

import {getResult} from "@/services/ant-design-pro/attribute";
import {Button, Input, Popconfirm} from "antd";

import {useLocation} from 'umi';
import CloudOrderShow from "@/components/CloudOrderShow";
import EditDialog from "@/pages/order/editDialog";
import {serachMiniByPhone} from "@/services/ant-design-pro/users";

const getAttr = async (key: string) => {
  let result = await getResult ( key, 1 );
  return result
}


const columns = [{
  title: "编号",
  key: "id",
  dataIndex: "id",
  search: false,
}, {
  title: "下单人员",
  key: "userName",
  dataIndex: "userName",


}, {
  title: "发货人员",
  key: "consignerName",
  dataIndex: "consignerName",


}, {
  title: "订单号",
  key: "sn",
  dataIndex: "sn"
}, {
  title: "第三方订单",
  key: "partySn",
  dataIndex: "partySn"
}, {
  title: "产品总数",
  key: "quantity",
  dataIndex: "quantity",
  search: false,
}, {
  title: "订单时间",
  key: "createDate",
  dataIndex: "createDate",
  valueType: 'dateRange',
  hideInTable: true,
  // initialValue: [moment().calendar(), moment().add(10, 'days').calendar()],
},  {
  title: "订单时间",
  key: "createDate",
  dataIndex: "createDate",
  valueType: 'dateTime',
  search: false
}, {
  title: "操作人员",
  key: "operatorName",
  dataIndex: "operatorName",
  search: false,

}]
const Page: React.FC = () => {

  const {query} = useLocation ();
  //findByPageOrderList
  const [pagination, setPagination] = useState<API.Pagination> ( defaultPagination () );
  const [params, setParams] = useState<any> ( query );
  const [fahuo, setFaHuo] = useState<any> ( "" );
  const {
    run: fetchByPageList,
    refresh,
    data: dataList,
    loading,
  } = useRequest ( findByPageConsignerOrderList, {
    debounceWait: 100,
    manual: true,
  } );





  useMount ( () => {
    columns[14] = {
      title: '操作',
      key: 'option',
      valueType: 'option',
      render: (_: any, row: { id: number | undefined; isPay: number | undefined; paymentStatus: number | undefined; }, index: any, action: any) => [
        <CloudOrderShow row={row} id={row.id}/>,
        row?.status != 3 ? <>
          <Button type="primary" danger
                  onClick={() => cancelConsignerOrder ( row.id ).then ( () => {
                    refresh ()
                  } )}>取消</Button>
          <Popconfirm
            title={<Input onChange={(e) => {
              let value = e.target.value;
              console.log ( value )
              setFaHuo ( value )
              console.log ( fahuo )
            }}/>}

            onConfirm={() => shipmentsConsigner ( row.id ).then ( (res) => {
              refresh ()
            } )}
            okText="Yes"
            cancelText="No"
          >
            <Button danger>发货</Button>
          </Popconfirm>
        </>: ""
      ],
    }
    //   const p = useLocation();
    console.log ( "onDataSourceChange", query )

    fetchByPageList ( params );
  } );


  return (<>

    <ProTable

      search={false}
      dataSource={dataList?.data?.list}
      loading={loading}
      pagination={paginationTool ( pagination, dataList?.data?.total, (data1: API.Pagination) => {
        setPagination ( data1 );

        fetchByPageList ( {...data1, ...params} );
      } )}
      search={{
        defaultCollapsed: false,
        labelWidth: 'auto',
      }}
      onSubmit={(params) => {
        // fetchByPageList ( params );
        console.log ( 'onSubmit', params );
        setParams ( {...params} )
        fetchByPageList ( params );
      }}
      cardBordered={true}
      columns={columns}
   /*   toolBarRender={()=>[<EditDialog taocanId={0}  onSuccess={refresh} />]}*/
    ></ProTable></>)
}

export default Page;
